# delimit ;
clear ;
set more off ;
version 12.0 ;

cd "replication" ;

* ***************************************************************************** ;
* Carpena, Fenella, and Bilal Zia. "The causal mechanism of financial education: 
* Evidence from mediation analysis." Journal of Economic Behavior & Organization 
* 177 (2020): 143-184.
* 
* This do-file creates Appendix Tables A1-A4 of the paper
* ***************************************************************************** ;

* ***************************************************************************** ;
* get endline data
* ***************************************************************************** ;

use "./data/endline.dta", clear ;

* add endline control variables ; 
merge 1:1 id using "./data/endline-controls.dta" ;
assert _merge == 3 ;
drop _merge ;

* merge with baseline ;
merge 1:1 id using "./data/baseline.dta" ;
tab _merge ;

* correction to the variable label ;
label var savings_bank "has bank savings account" ;

* ***************************************************************************** ;
* sample selection
* ***************************************************************************** ;

* drop households with baseline but no endline ;
drop if _merge == 2 ; 
drop _merge ;

* drop households with no financial knowledge scores ; 
egen temp_knowl = rowmean(numer_fin_return-attit_budget) ;
drop if temp_knowl == . ;
drop temp_knowl ; 

* identify respondents with endline measures of numeracy ;
merge 1:1 id using "./data/financial-knowledge-short-term", keepusing(id) ;
tab _merge ;
keep if _merge == 3 ;
drop _merge ;

* drop households with missing discount rate (control variable) ;
drop if disc_rate == . ;

* ***************************************************************************** ;
* create financial knowledge scores
* ***************************************************************************** ;

egen end_numeracy = rowmean(numer_fin_return-numer_int_rate) ;
egen end_awareness = rowmean(aware_budget-aware_unprod) ;
egen end_attitudes = rowmean(attit_suggest_ins-attit_budget) ;

* ***************************************************************************** ;
* create treatment dummies
* ***************************************************************************** ;

gen flcounsgoal = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 1)  ;
gen flcouns = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 0) ;
gen flgoal = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 1) ;
gen fl = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 0) ;
gen control = (fin_ed_treatment == 0 & couns_treatment == 0 & goal_treatment == 0) ;

* ***************************************************************************** ;
* DEFINE PROGRAM FOR SEQUENTIAL G ESTIMATOR ;
* ***************************************************************************** ;

* syntax: outcome 1, treatment 2, mediator 3 ;
program drop _all ;

program define deboot, rclass ;
	syntax varlist, sample(string) precvars(string) [postcvars(string)] ;
	tokenize `varlist' ; 
	
	********************************** ;
	* CALCULATE DIRECT EFFECT 
	********************************** ;
	
	* step 1: regress Y on T, M, pre controls, post controls ;
	areg `1' `2' `3' `precvars' `postcvars' if `sample' , a(strata) clu(wave_class) ;
	
	* step 2a: demediate Y ;
	capture drop `1'_tilde ;
	gen `1'_tilde = `1' - _b[`3']*`3' if `sample' ;
	
	* step 2b: regress Y tilde on T and pre controls ;
	areg `1'_tilde `2' `precvars' if `sample', a(strata) clu(wave_class) ;
	
	local deffect = _b[`2'] ;
	return scalar deffect = _b[`2'];

	********************************** ;
	* CALCULATE ATE
	********************************** ;
	areg `1' `2' `precvars' if `sample', a(strata) clu(wave_class) ;
	
	local ate = _b[`2'] ;
	return scalar ate = _b[`2'] ;
	
	********************************** ;
	* CALCULATE ACME = ATE - DEFFECT
	********************************** ;
	return scalar acme = `ate' - `deffect' ;
	
end ;

* ***************************************************************************** ;
* control variables, baseline (pre) and endline (post)
* ***************************************************************************** ;

local pre_cvars has_hard_time_saving interested_in_financial inconsistent disc_rate riskaverse  ;
local post_cvars has_hard_time_saving_end inconsistent_end disc_rate_end riskaverse_end ; 

* ***************************************************************************** ;
* rename variables to make them shorter ;
* ***************************************************************************** ;

ren budget_helpful bdg_help ;
ren budget_tried bdg_tried ;
ren budget_regular bdg_reg ;
ren savings_informal sav_inf ;
ren savings_bank sav_bnk ;
ren loan_purpose_bus_educ loan_bus ; 
ren ins_life ins_life ;

* ***************************************************************************** ;
* APPENDIX TABLE A1: ADE, FL only 
* ***************************************************************************** ;

est drop _all ;

local t fl ;
local outcomes bdg_tried ; 

* loop overall outcomes and mediators ; 
foreach y in `outcomes' { ;
	foreach m in end_numeracy end_awareness end_attitudes { ;
	
		di "******** REGRESSION TO OBTAIN ACDE w/ BOOTSTRAPPED SEs ************" ;

		di "*Panel A: pre-treatment controls only" ;
		bootstrap acme=r(acme) deffect=r(deffect) , nodots reps(1000) seed(41574): 
		deboot `y' `t' `m' , sample(`t'|control) precvars(`pre_cvars') ;
		est sto pre`y'_`m' ;

		di "*Panel B: pre-treatment and post-treatment controls" ;
		bootstrap acme=r(acme) deffect=r(deffect) , nodots reps(1000) seed(41574): 
		deboot `y' `t' `m' , sample(`t'|control) precvars(`pre_cvars') postcvars(`post_cvars') ;
		est sto post`y'_`m' ;
		
	} ;
} ;

* output: panel a; 
shell printf "********** Appendix Table A1. Panel A **********\n" > "./output/appendix-table-A01-A04.csv" ;
shell printf "`t' - Pre-T controls only \r\n" >> "./output/appendix-table-A01-A04.csv" ;
esttab pre*  using "./output/appendix-table-A01-A04.csv",
		append msign(-) depvars label
		cells(b(star fmt(%9.3f %9.3f)) se(par))
		star(* 0.10 ** 0.05 *** 0.01) 
		stats(N, fmt(%9.0f)
		labels("N"))
		mtitles ;
shell printf "\r\n" >> "./output/appendix-table-A01-A04.csv" ;

* output: panel b; 
shell printf "********** Appendix Table A1. Panel B **********\n" >> "./output/appendix-table-A01-A04.csv" ;
shell printf "`t' - Pre-T and Post-T controls \r\n" >> "./output/appendix-table-A01-A04.csv" ;
esttab post*  using "./output/appendix-table-A01-A04.csv",
		append msign(-) depvars label
		cells(b(star fmt(%9.3f %9.3f)) se(par))
		star(* 0.10 ** 0.05 *** 0.01) 
		stats(N, fmt(%9.0f)
		labels("N"))
		mtitles ;
		
shell printf "\r\n" >> "./output/appendix-table-A01-A04.csv" ;

* ***************************************************************************** ;
* APPENDIX TABLE A2: ADE, FL goal only 
* ***************************************************************************** ;

est drop _all ;

local t flgoal ;
local outcomes bdg_tried sav_bnk ; 

* loop overall outcomes and mediators ; 
foreach y in `outcomes' { ;
	foreach m in end_numeracy end_awareness end_attitudes { ;
	
		di "******** REGRESSION TO OBTAIN ACDE w/ BOOTSTRAPPED SEs ************" ;

		di "*Panel A: pre-treatment controls only" ;
		bootstrap acme=r(acme) deffect=r(deffect) , nodots reps(1000) seed(41574): 
		deboot `y' `t' `m' , sample(`t'|control) precvars(`pre_cvars') ;
		est sto pre`y'_`m' ;

		di "*Panel B: pre-treatment and post-treatment controls" ;
		bootstrap acme=r(acme) deffect=r(deffect) , nodots reps(1000) seed(41574): 
		deboot `y' `t' `m' , sample(`t'|control) precvars(`pre_cvars') postcvars(`post_cvars') ;
		est sto post`y'_`m' ;
		
	} ;
} ;

* output: panel a; 
shell printf "********** Appendix Table A2. Panel A **********\n" >> "./output/appendix-table-A01-A04.csv" ;
shell printf "`t' - Pre-T controls only \r\n" >> "./output/appendix-table-A01-A04.csv" ;
esttab pre*  using "./output/appendix-table-A01-A04.csv",
		append msign(-) depvars label
		cells(b(star fmt(%9.3f %9.3f)) se(par))
		star(* 0.10 ** 0.05 *** 0.01) 
		stats(N, fmt(%9.0f)
		labels("N"))
		mtitles ;
shell printf "\r\n" >> "./output/appendix-table-A01-A04.csv" ;

* output: panel b; 
shell printf "********** Appendix Table A2. Panel B **********\n" >> "./output/appendix-table-A01-A04.csv" ;
shell printf "`t' - Pre-T and Post-T controls \r\n" >> "./output/appendix-table-A01-A04.csv" ;
esttab post*  using "./output/appendix-table-A01-A04.csv",
		append msign(-) depvars label
		cells(b(star fmt(%9.3f %9.3f)) se(par))
		star(* 0.10 ** 0.05 *** 0.01) 
		stats(N, fmt(%9.0f)
		labels("N"))
		mtitles ;	
shell printf "\r\n" >> "./output/appendix-table-A01-A04.csv" ;

* ***************************************************************************** ;
* APPENDIX TABLE A3: ADE, FL couns only 
* ***************************************************************************** ;

est drop _all ;

local t flcouns ;
local outcomes bdg_tried sav_bnk loan_bus ins_life ; 

* loop overall outcomes and mediators ; 
foreach y in `outcomes' { ;
	foreach m in end_numeracy end_awareness end_attitudes { ;
	
		di "******** REGRESSION TO OBTAIN ACDE w/ BOOTSTRAPPED SEs ************" ;

		di "*Panel A: pre-treatment controls only" ;
		bootstrap acme=r(acme) deffect=r(deffect) , nodots reps(1000) seed(41574): 
		deboot `y' `t' `m' , sample(`t'|control) precvars(`pre_cvars') ;
		est sto pre`y'_`m' ;

		di "*Panel B: pre-treatment and post-treatment controls" ;
		bootstrap acme=r(acme) deffect=r(deffect) , nodots reps(1000) seed(41574): 
		deboot `y' `t' `m' , sample(`t'|control) precvars(`pre_cvars') postcvars(`post_cvars') ;
		est sto post`y'_`m' ;
		
	} ;
} ;

* output: panel a; 
shell printf "********** Appendix Table A3. Panel A **********\n" >> "./output/appendix-table-A01-A04.csv" ;
shell printf "`t' - Pre-T controls only \r\n" >> "./output/appendix-table-A01-A04.csv" ;
esttab pre*  using "./output/appendix-table-A01-A04.csv",
		append msign(-) depvars label
		cells(b(star fmt(%9.3f %9.3f)) se(par))
		star(* 0.10 ** 0.05 *** 0.01) 
		stats(N, fmt(%9.0f)
		labels("N"))
		mtitles ;
shell printf "\r\n" >> "./output/appendix-table-A01-A04.csv" ;

* output: panel b; 
shell printf "********** Appendix Table A3. Panel B **********\n" >> "./output/appendix-table-A01-A04.csv" ;
shell printf "`t' - Pre-T and Post-T controls \r\n" >> "./output/appendix-table-A01-A04.csv" ;
esttab post*  using "./output/appendix-table-A01-A04.csv",
		append msign(-) depvars label
		cells(b(star fmt(%9.3f %9.3f)) se(par))
		star(* 0.10 ** 0.05 *** 0.01) 
		stats(N, fmt(%9.0f)
		labels("N"))
		mtitles ;	
shell printf "\r\n" >> "./output/appendix-table-A01-A04.csv" ;

* ***************************************************************************** ;
* APPENDIX TABLE A4: ADE, FL couns goal
* ***************************************************************************** ;

est drop _all ;

local t flcounsgoal ;
local outcomes bdg_tried sav_bnk ins_life ; 

* loop overall outcomes and mediators ; 
foreach y in `outcomes' { ;
	foreach m in end_numeracy end_awareness end_attitudes { ;
	
		di "******** REGRESSION TO OBTAIN ACDE w/ BOOTSTRAPPED SEs ************" ;

		di "*Panel A: pre-treatment controls only" ;
		bootstrap acme=r(acme) deffect=r(deffect) , nodots reps(1000) seed(41574): 
		deboot `y' `t' `m' , sample(`t'|control) precvars(`pre_cvars') ;
		est sto pre`y'_`m' ;

		di "*Panel B: pre-treatment and post-treatment controls" ;
		bootstrap acme=r(acme) deffect=r(deffect) , nodots reps(1000) seed(41574): 
		deboot `y' `t' `m' , sample(`t'|control) precvars(`pre_cvars') postcvars(`post_cvars') ;
		est sto post`y'_`m' ;
		
	} ;
} ;

* output: panel a; 
shell printf "********** Appendix Table A4. Panel A **********\n" >> "./output/appendix-table-A01-A04.csv" ;
shell printf "`t' - Pre-T controls only \r\n" >> "./output/appendix-table-A01-A04.csv" ;
esttab pre*  using "./output/appendix-table-A01-A04.csv",
		append msign(-) depvars label
		cells(b(star fmt(%9.3f %9.3f)) se(par))
		star(* 0.10 ** 0.05 *** 0.01) 
		stats(N, fmt(%9.0f)
		labels("N"))
		mtitles ;
shell printf "\r\n" >> "./output/appendix-table-A01-A04.csv" ;

* output: panel b; 
shell printf "********** Appendix Table A4. Panel B **********\n" >> "./output/appendix-table-A01-A04.csv" ;
shell printf "`t' - Pre-T and Post-T controls \r\n" >> "./output/appendix-table-A01-A04.csv" ;
esttab post*  using "./output/appendix-table-A01-A04.csv",
		append msign(-) depvars label
		cells(b(star fmt(%9.3f %9.3f)) se(par))
		star(* 0.10 ** 0.05 *** 0.01) 
		stats(N, fmt(%9.0f)
		labels("N"))
		mtitles ;	
shell printf "\r\n" >> "./output/appendix-table-A01-A04.csv" ;

exit ;
